package core.network.client2game.handler.room;

import BaseCommon.CommLog;
import business.global.room.RoomMgr;
import business.global.room.base.AbsBaseRoom;
import business.global.room.base.AbsRoomPos;
import business.player.Player;
import business.player.feature.PlayerRoom;
import com.ddm.server.websocket.def.ErrorCode;
import com.ddm.server.websocket.handler.requset.WebSocketRequest;
import com.google.gson.Gson;
import core.network.client2game.handler.PlayerHandler;
import jsproto.c2s.iclass.room.CBase_GetRoomInfo;
import java.io.IOException;
import java.util.Objects;

public abstract class CBaseGetRoomInfo extends PlayerHandler{

    @Override
    public void handle(Player player, WebSocketRequest request, String message) throws IOException {
        final CBase_GetRoomInfo req = new Gson().fromJson(message, CBase_GetRoomInfo.class);
        AbsBaseRoom room = RoomMgr.getInstance().getRoom(req.getRoomID());
        // 检查房间是否存在
        if (Objects.isNull(room)) {
            if (req.getRoomID() ==  player.getFeature(PlayerRoom.class).getRoomID()) {
                player.onGMExitRoom();
                CommLog.error("error game room roomId:{},pid:{}",req.getRoomID(),player.getPid());
            }
            request.error(ErrorCode.NotFind_Room, "CBaseGetRoomInfo null == room");
            return;
        }
        AbsRoomPos roomPos = room.getRoomPosMgr().getPosByPid(player.getPid());
        // 检查玩家是否在房间中
        if (Objects.isNull(roomPos)) {
            if (req.getRoomID() ==  player.getFeature(PlayerRoom.class).getRoomID()) {
                player.onGMExitRoom();
                CommLog.error("error game roomPos roomId:{},pid:{}",req.getRoomID(),player.getPid());
            }
            request.error(ErrorCode.NotFind_Player, "CBaseGetRoomInfo null == roomPos");
            return;
        }
        if (roomPos.isLostConnect()) {
            room.lostConnect(player.getPid(),false);
        } else if (roomPos.isShowLeave()) {
            room.opShowLeave(player.getPid(), false);
        }
        // 获取房间信息
        request.response(room.getRoomInfo(player.getPid()));// 无账号情况下返回为空
    }
}
